home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gnushogi.lha / gnushogi-1.1 / src / dspcom.c < prev    next >
C/C++ Source or Header  |  1993-04-20  |  35KB  |  1,525 lines

  1. /*
  2.  * dspcom.c - C source for GNU SHOGI
  3.  *
  4.  * Copyright (c) 1993 Matthias Mutz
  5.  *
  6.  * GNU SHOGI is based on GNU CHESS
  7.  *
  8.  * Copyright (c) 1988,1989,1990 John Stanback
  9.  * Copyright (c) 1992 Free Software Foundation
  10.  *
  11.  * This file is part of GNU SHOGI.
  12.  *
  13.  * GNU Shogi is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation; either version 1, or (at your option)
  16.  * any later version.
  17.  *
  18.  * GNU Shogi is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  * GNU General Public License for more details.
  22.  *
  23.  * You should have received a copy of the GNU General Public License
  24.  * along with GNU Shogi; see the file COPYING.  If not, write to
  25.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  */
  27.  
  28. #include "gnushogi.h"
  29. #include "ataks.h"
  30. #ifdef DEBUG40
  31. int whichway;
  32. #endif
  33. #if defined HASGETTIMEOFDAY && !defined THINK_C
  34. #include <sys/time.h>
  35. #endif
  36. extern char *version, *patchlevel;
  37. extern unsigned int TTadd;
  38. char mvstr[4][6];
  39. char *InPtr;
  40. int    InBackground = false;
  41.  
  42.  
  43. #include <ctype.h>
  44. #include <signal.h>
  45. #if defined THINK_C
  46. #include <time.h>
  47. #define BOOKTEST
  48. #elif defined MSDOS
  49. #include <dos.h>
  50. #include <conio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <time.h>
  54. #else
  55. #include <sys/param.h>
  56. #include <sys/types.h>
  57. #include <sys/file.h>
  58. #include <sys/ioctl.h>
  59. #endif
  60.  
  61. #ifndef EVALFILE
  62. #ifdef THINK_C
  63. #define EVALFILE "EVAL"
  64. #else
  65. #define EVALFILE "/tmp/EVAL"
  66. #endif
  67. #endif
  68.  
  69. #if defined DEBUG || defined BOOKTEST
  70.  
  71. void
  72. movealgbr (short int m, char *s)
  73. {
  74.     unsigned int f, t;
  75.     short piece = 0, flag = 0;
  76.     f = m >> 8 & 0x7f;
  77.     t = m & 0xff;
  78.     if ( f > NO_SQUARES )
  79.       { short piece = f - NO_SQUARES;
  80.         if ( f > NO_PIECES ) f -= NO_PIECES;
  81.         flag = (dropmask | piece );
  82.       }
  83.     if ( t & 0x80 )
  84.       {
  85.         flag |= promote;
  86.         t &= 0x7f;
  87.       }
  88.     if ( flag & dropmask )
  89.       {
  90.         *s = pxx[piece]; s++;
  91.         *s = '*'; s++;
  92.         *s = cxx[column (t)]; s++;
  93.         *s = rxx[row (t)]; s++;
  94.       }
  95.     else
  96.       {
  97.         *s = cxx[column (f)]; s++;
  98.         *s = rxx[row (f)]; s++;
  99.         *s = cxx[column (t)]; s++;
  100.         *s = rxx[row (t)]; s++;
  101.         if ( flag & promote )
  102.           {
  103.             *s = '+'; s++;
  104.           }
  105.       }
  106.     if (m & 0x8000)
  107.       {
  108.         *s = '?'; s++;
  109.       }
  110.     *s = '\0';
  111. }
  112.  
  113. #endif
  114.  
  115.  
  116. void
  117. algbr (short int f, short int t, short int flag)
  118.  
  119.  
  120. /*
  121.  * Generate move strings in different formats.
  122.  */
  123.  
  124. {
  125.   int m3p;
  126.  
  127.   if ( f > NO_SQUARES ) 
  128.     { short piece;
  129.       piece = f - NO_SQUARES;
  130.       if ( f > (NO_SQUARES+NO_PIECES) )
  131.         piece -= NO_PIECES;
  132.       flag = (dropmask | piece); 
  133.     }
  134.   if ( (t & 0x80) != 0 )
  135.     {
  136.       flag |= promote;
  137.       t &= 0x7f;
  138.     }
  139.   if ( f == t && (f != 0 || t != 0) ) 
  140.     { 
  141. #if !defined BAREBONES
  142.       printz("error in algbr: FROM=TO=%d, flag=0x%4x\n",t,flag);
  143. #endif
  144.       mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  145.     }
  146.   else
  147.   if ( (flag & dropmask) != 0 )
  148.     { short piece = flag & pmask;
  149.       mvstr[0][0] = pxx[piece];
  150.       mvstr[0][1] = '*';
  151.       mvstr[0][2] = cxx[column (t)];
  152.       mvstr[0][3] = rxx[row (t)];
  153.       mvstr[0][4] = '\0';
  154.       strcpy (mvstr[1], mvstr[0]);
  155.       strcpy (mvstr[2], mvstr[0]);
  156.       strcpy (mvstr[3], mvstr[0]);
  157.     }
  158.   else
  159.   if (f != 0 || t != 0)
  160.     {
  161.       /* algebraic notation */
  162.       mvstr[0][0] = cxx[column (f)];
  163.       mvstr[0][1] = rxx[row (f)];
  164.       mvstr[0][2] = cxx[column (t)];
  165.       mvstr[0][3] = rxx[row (t)];
  166.       mvstr[0][4] = mvstr[3][0] = '\0';
  167.       mvstr[1][0] = pxx[board[f]];
  168.  
  169.       mvstr[2][0] = mvstr[1][0];
  170.       mvstr[2][1] = mvstr[0][1]; 
  171.  
  172.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  173.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  174.       mvstr[2][4] = mvstr[1][3] = '\0';
  175.       strcpy (mvstr[3], mvstr[2]);
  176.       mvstr[3][1] = mvstr[0][0];
  177.       if (flag & promote)
  178.         {
  179.           strcat(mvstr[0], "+");
  180.           strcat(mvstr[1], "+");
  181.           strcat(mvstr[2], "+");
  182.           strcat(mvstr[3], "+");
  183.         }
  184.     }
  185.   else
  186.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  187. }
  188.            
  189.  
  190. int
  191. VerifyMove (char *s, short int iop, short unsigned int *mv)
  192.  
  193. /*
  194.  * Compare the string 's' to the list of legal moves available for the
  195.  * opponent. If a match is found, make the move on the board.
  196.  */
  197.  
  198. {
  199.   static short pnt, tempb, tempc, tempsf, tempst, cnt;
  200.   static struct leaf xnode;
  201.   struct leaf *node;
  202.  
  203.   *mv = 0;
  204.   if (iop == 2)
  205.     {
  206.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  207.       return (false);
  208.     }
  209.   cnt = 0;
  210.   MoveList (opponent, 2);
  211.   pnt = TrPnt[2];
  212.   while (pnt < TrPnt[3])
  213.     {
  214.       node = &Tree[pnt++];
  215.       algbr (node->f, node->t, (short) node->flags);
  216.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  217.       strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  218.     {
  219.       cnt++;
  220.       xnode = *node;
  221.     }
  222.     }
  223.   if (cnt == 1)
  224.     {
  225.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  226.       if (SqAtakd (PieceList[opponent][0], computer))
  227.     {
  228.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  229. #ifdef NONDSP
  230. /* Illegal move in check */
  231.       printz (CP[77]);
  232.       printz ("\n");
  233. #else
  234. /* Illegal move in check */
  235.       ShowMessage (CP[77]);
  236. #endif
  237.       return (false);
  238.     }
  239.       else
  240.     {
  241.       if (iop == 1)
  242.         return (true);
  243.       UpdateDisplay (xnode.f, xnode.t, 0, (short) xnode.flags);
  244.       GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  245.       GameList[GameCnt].nodes = 0;
  246.       ElapsedTime (1);
  247.       GameList[GameCnt].time = (short) (et+50)/100;
  248.       if (TCflag)
  249.         {
  250.           TimeControl.clock[opponent] -= et;
  251.           timeopp[oppptr] = et;
  252.           --TimeControl.moves[opponent];
  253.         }
  254.       *mv = (xnode.f << 8) | xnode.t;
  255.       algbr (xnode.f, xnode.t, false);
  256.       return (true);
  257.     }
  258.     }
  259. #ifdef NONDSP
  260. /* Illegal move */
  261.   printz (CP[75], s);
  262. #ifdef DEBUG8
  263.   if (1)
  264.     {
  265.       FILE *D;
  266.       int r, c, side, l;
  267.       extern unsigned short int PrVar[];
  268.       extern void debug_position();
  269.       D = fopen ("/tmp/DEBUG", "a+");
  270.       pnt = TrPnt[2];
  271.       fprintf (D, "resp = %d\n", ResponseTime);
  272.       fprintf (D, "iop = %d\n", iop);
  273.       fprintf (D, "matches = %d\n", cnt);
  274.       algbr (hint >> 8, hint & 0xff, (short) 0);
  275.       fprintf (D, "hint %s\n", mvstr[0]);
  276.       fprintf (D, "inout move is %s\n", s);
  277.       for (r = 1; PrVar[r]; r++)
  278.     {
  279.       algbr (PrVar[r] >> 8, PrVar[r] & 0xff, (short) 0);
  280.       fprintf (D, " %s", mvstr[0]);
  281.     }
  282.       fprintf (D, "\n");
  283.       fprintf (D, "legal move are \n");
  284.       while (pnt < TrPnt[3])
  285.     {
  286.       node = &Tree[pnt++];
  287.       algbr (node->f, node->t, (short) node->flags);
  288.       fprintf (D, "%s %s %s %s\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3]);
  289.     }
  290.       debug_position (D);
  291.       fclose (D);
  292.       abort ();
  293.     }
  294. #endif
  295. #else
  296. /* Illegal move */
  297.   ShowMessage (CP[76]);
  298. #endif
  299. #if !defined BAREBONES
  300.   if (cnt > 1)
  301.     ShowMessage (CP[32]);
  302. #endif 
  303.   return (false);
  304. }
  305.  
  306. int
  307. parser (char *f, int side)
  308. {
  309.   int c1, r1, c2, r2;
  310.  
  311.   c1 = '9' - f[0];
  312.   r1 = 'a' - f[1];
  313.   c2 = '9' - f[2];
  314.   r2 = 'i' - f[3];
  315.   return (locn (r1, c1) << 8) | locn (r2, c2);
  316.   /*NOTREACHED*/
  317. }
  318.  
  319. void
  320. GetGame (void)
  321. {
  322.   FILE *fd;
  323.   char fname[256], *p;
  324.   int c, i, j;
  325.   short sq;
  326.   short side, isp;
  327. /* enter file name */
  328.   ShowMessage (CP[63]);
  329.   scanz ("%s", fname);
  330. /* shogi.000 */
  331.   if (fname[0] == '\0')
  332.     strcpy (fname, CP[137]);
  333.   if ((fd = fopen (fname, "r")) != NULL)
  334.     {
  335.       NewGame ();
  336.       fgets (fname, 256, fd);
  337.       computer = opponent = black;
  338.       InPtr = fname;
  339.       skip ();
  340.       if (*InPtr == 'c')
  341.     computer = white;
  342.       else
  343.     opponent = white;
  344.       skip ();
  345.       skip ();
  346.       skip ();
  347.       Game50 = atoi (InPtr);
  348.       fgets (fname, 256, fd);
  349.       InPtr = &fname[11];
  350.       skipb ();
  351.       TCflag = atoi (InPtr);
  352.       skip ();
  353.       InPtr += 14;
  354.       skipb ();
  355.       OperatorTime = atoi (InPtr);
  356.       fgets (fname, 256, fd);
  357.       InPtr = &fname[11];
  358.       skipb ();
  359.       TimeControl.clock[black] = atol (InPtr);
  360.       skip ();
  361.       skip ();
  362.       TimeControl.moves[black] = atoi (InPtr);
  363.       fgets (fname, 256, fd);
  364.       InPtr = &fname[11];
  365.       skipb ();
  366.       TimeControl.clock[white] = atol (InPtr);
  367.       skip ();
  368.       skip ();
  369.       TimeControl.moves[white] = atoi (InPtr);
  370.       for (i = NO_ROWS-1; i > -1; i--)
  371.     {
  372.       fgets (fname, 256, fd);
  373.       p = &fname[2];
  374.       InPtr = &fname[21];
  375.       for (j = 0; j < NO_COLS; j++)
  376.         {
  377.           sq = i * NO_COLS + j;
  378.           isp = ( *p == '+' );
  379.           p++;
  380.           if (*p == '.')
  381.         {
  382.           board[sq] = no_piece;
  383.           color[sq] = neutral;
  384.         }
  385.           else
  386.         {
  387.           for (c = 0; c < NO_PIECES; c++)
  388.             {
  389.               if (*p == pxx[c])
  390.             {
  391.               if ( isp )
  392.                 board[sq] = promoted[c];
  393.               else
  394.                 board[sq] = unpromoted[c];
  395.               color[sq] = white;
  396.             }
  397.             }
  398.           for (c = 0; c < NO_PIECES; c++)
  399.             {
  400.               if (*p == qxx[c])
  401.             {
  402.               if ( isp )
  403.                 board[sq] = promoted[c];
  404.               else
  405.                 board[sq] = unpromoted[c];
  406.               color[sq] = black;
  407.             }
  408.             }
  409.         }
  410.           p++;
  411.           Mvboard[sq] = atoi (InPtr);
  412.           skip ();
  413.         }
  414.     }
  415.       fgets (fname, 256, fd);  /* 9 8 7 ... */
  416.       fgets (fname, 256, fd);  /* p l n ... */
  417.       ClearCaptured ();
  418.       for ( side = 0; side <= 1; side++ ) {
  419.         fgets (fname, 256, fd);
  420.     InPtr = fname;
  421.     skipb ();
  422.         Captured[side][pawn] = atoi (InPtr);
  423.     skip ();
  424.         Captured[side][lance] = atoi (InPtr);
  425.     skip ();
  426.         Captured[side][knight] = atoi (InPtr);
  427.     skip ();
  428.         Captured[side][silver] = atoi (InPtr);
  429.     skip ();
  430.         Captured[side][gold] = atoi (InPtr);
  431.     skip ();
  432.         Captured[side][bishop] = atoi (InPtr);
  433.     skip ();
  434.         Captured[side][rook] = atoi (InPtr);
  435.     skip ();
  436.         Captured[side][king] = atoi (InPtr);
  437.       }
  438.       GameCnt = 0;
  439.       flag.regularstart = true;
  440.       Book = BOOKFAIL;
  441.       fgets (fname, 256, fd);   /*  move score ... */
  442.       while (fgets (fname, 256, fd))
  443.     {
  444.       struct GameRec *g;
  445.       int side = computer;
  446.  
  447.       side = side ^ 1;
  448.       ++GameCnt;
  449.       InPtr = fname;
  450.       skipb ();
  451.       g = &GameList[GameCnt];
  452.       g->gmove = parser (InPtr, side);
  453.       skip ();
  454.       g->score = atoi (InPtr);
  455.       skip ();
  456.       g->depth = atoi (InPtr);
  457.       skip ();
  458.       g->nodes = atol (InPtr);
  459.       skip ();
  460.       g->time = atol (InPtr);
  461.       skip ();
  462.       g->flags = c = atoi (InPtr);
  463.       skip ();
  464.       g->hashkey = strtol (InPtr, (char **) NULL, 16);
  465.       skip ();
  466.       g->hashbd = strtol (InPtr, (char **) NULL, 16);
  467.       g->piece = no_piece;
  468.       g->color = neutral;
  469.       if (c & capture)
  470.         {
  471.           skip ();
  472.           for (c = 0; c < NO_COLS; c++)
  473.             if (pxx[c] == *InPtr)
  474.               break;
  475.           g->piece = c;
  476.           }
  477.       skip ();
  478.       g->color = ((*InPtr == CP[119][0]) ? white : black);
  479.       skip ();
  480.       if ( *InPtr == '+' ) 
  481.         g->piece = promoted[g->piece];
  482.     }
  483.       if (TimeControl.clock[black] > 0)
  484.     TCflag = true;
  485.       fclose (fd);
  486.     }
  487.   ZeroRPT ();
  488.   InitializeStats ();
  489.   UpdateDisplay (0, 0, 1, 0);
  490.   Sdepth = 0;
  491.   hint = 0;
  492. }
  493.  
  494. void
  495. GetXGame (void)
  496. {
  497.   FILE *fd;
  498.   char fname[256], *p;
  499.   int c, i, j;
  500.   short sq;
  501.   short side, isp;
  502. /* Enter file name */
  503.   ShowMessage (CP[63]);
  504.   scanz ("%s", fname);
  505.   if (fname[0] == '\0')
  506. /* XSHOGI.position.read*/
  507.     strcpy (fname, CP[205]);
  508.   if ((fd = fopen (fname, "r")) != NULL)
  509.     {
  510.       NewGame ();
  511.       flag.regularstart = false;
  512.       Book = false;
  513.       /* xshogi position file ... */
  514.       fgets (fname, 256, fd);
  515. #ifdef notdef
  516.       fname[6] = '\0';
  517.       if (strcmp (fname, CP[206]))
  518.     return;
  519. #endif
  520.       /* -- empty line -- */
  521.       fgets (fname, 256, fd);
  522.       /* -- empty line -- */
  523.       fgets (fname, 256, fd);
  524.       for (i = NO_ROWS-1; i > -1; i--)
  525.     {
  526.       fgets (fname, 256, fd);
  527.       p = fname;
  528.       for (j = 0; j < NO_COLS; j++)
  529.         {
  530.           sq = i * NO_COLS + j;
  531.           isp = ( *p == '+' );
  532.           p++;
  533.           if (*p == '.')
  534.         {
  535.           board[sq] = no_piece;
  536.           color[sq] = neutral;
  537.         }
  538.           else
  539.         {
  540.           for (c = 0; c < NO_PIECES; c++)
  541.             {
  542.               if (*p == qxx[c])
  543.             {
  544.               if ( isp )
  545.                 board[sq] = promoted[c];
  546.               else
  547.                 board[sq] = unpromoted[c];
  548.               color[sq] = white;
  549.             }
  550.             }
  551.           for (c = 0; c < NO_PIECES; c++)
  552.             {
  553.               if (*p == pxx[c])
  554.             {
  555.               if ( isp )
  556.                 board[sq] = promoted[c];
  557.               else
  558.                 board[sq] = unpromoted[c];
  559.               color[sq] = black;
  560.             }
  561.             }
  562.         }
  563.           p++;
  564.         }
  565.     }
  566.     ClearCaptured ();
  567.         for ( side = 0; side <= 1; side++ ) {
  568.       fgets (fname, 256, fd);
  569.       InPtr = fname;
  570.       Captured[side][pawn]   = atoi (InPtr);
  571.       skip ();
  572.       Captured[side][lance]  = atoi (InPtr);
  573.       skip ();
  574.       Captured[side][knight] = atoi (InPtr);
  575.       skip ();
  576.       Captured[side][silver] = atoi (InPtr);
  577.       skip ();
  578.       Captured[side][gold]   = atoi (InPtr);
  579.       skip ();
  580.       Captured[side][bishop] = atoi (InPtr);
  581.       skip ();
  582.       Captured[side][rook]   = atoi (InPtr);
  583.       skip ();
  584.       Captured[side][king]   = atoi (InPtr);
  585.         };
  586.         if (fgets(fname, 256, fd) != NULL && strncmp(fname, "white", 5) == 0)
  587.         {
  588.           computer = black;
  589.           opponent = white;
  590.           xwndw = BXWNDW;
  591.     }
  592.       fclose (fd);
  593.     }
  594.   ZeroRPT ();
  595.   InitializeStats ();
  596.   UpdateDisplay (0, 0, 1, 0);
  597.   Sdepth = 0;
  598.   hint = 0;
  599. }
  600.  
  601. void
  602. SaveGame (void)
  603. {
  604.   FILE *fd;
  605.   char fname[256];
  606.   short sq, i, c, f, t;
  607.   char p;
  608.   short side, piece;
  609.  
  610.   if (savefile[0])
  611.     strcpy (fname, savefile);
  612.   else
  613.     {
  614. /* Enter file name*/
  615.       ShowMessage (CP[63]);
  616.       scanz ("%s", fname);
  617.     }
  618.  
  619.   if (fname[0] == '\0')
  620. /* shogi.000 */
  621.     strcpy (fname, CP[137]);
  622.   if ((fd = fopen (fname, "w")) != NULL)
  623.     {
  624.       char *b, *w;
  625.  
  626.       b = w = CP[74];
  627.       if (computer == white)
  628.     w = CP[141];
  629.       if (computer == black)
  630.     b = CP[141];
  631.       fprintf (fd, CP[37], w, b, Game50);
  632.       fprintf (fd, CP[111], TCflag, OperatorTime);
  633.       fprintf (fd, CP[117],
  634.            TimeControl.clock[black], TimeControl.moves[black],
  635.            TimeControl.clock[white], TimeControl.moves[white]);
  636.       for (i = NO_ROWS-1; i > -1; i--)
  637.     {
  638.       fprintf (fd, "%c ", 'i' - i);
  639.       for (c = 0; c < NO_COLS; c++)
  640.         { 
  641.           sq = i * NO_COLS + c;
  642.           piece = board[sq];
  643.           p = is_promoted[piece] ? '+' : ' ';
  644.              fprintf (fd, "%c", p);
  645.           switch (color[sq])
  646.         {
  647.         case white:
  648.           p = pxx[piece];
  649.           break;
  650.         case black:
  651.           p = qxx[piece];
  652.           break;
  653.         default:
  654.           p = '.';
  655.         }
  656.           fprintf (fd, "%c", p);
  657.         }
  658.       for (f = i * NO_COLS; f < i * NO_COLS + NO_ROWS; f++)
  659.         fprintf (fd, " %d", Mvboard[f]);
  660.       fprintf (fd, "\n");
  661.     }
  662.       fprintf (fd, "   9 8 7 6 5 4 3 2 1\n");
  663.       fprintf (fd, "  p  l  n  s  g  b  r  k\n");
  664.       for ( side = 0; side <= 1; side++ ) {
  665.         fprintf (fd, " %2d", Captured[side][pawn]); 
  666.         fprintf (fd, " %2d", Captured[side][lance]); 
  667.         fprintf (fd, " %2d", Captured[side][knight]); 
  668.         fprintf (fd, " %2d", Captured[side][silver]); 
  669.         fprintf (fd, " %2d", Captured[side][gold]); 
  670.         fprintf (fd, " %2d", Captured[side][bishop]); 
  671.         fprintf (fd, " %2d", Captured[side][rook]); 
  672.         fprintf (fd, " %2d", Captured[side][king]); 
  673.         fprintf (fd, "\n");
  674.       }
  675.       fprintf (fd, CP[126]);
  676.       for (i = 1; i <= GameCnt; i++)
  677.     {
  678.       struct GameRec *g = &GameList[i];
  679.  
  680.       f = g->gmove >> 8;
  681.       t = (g->gmove & 0xFF);
  682.       algbr (f, t, g->flags);
  683.       fprintf (fd, "%5s %6d %5d %7ld %6ld %5d  %#08lx %#08lx  %c %s %c\n",
  684.            mvstr[0], g->score, g->depth,
  685.            g->nodes, g->time, g->flags, g->hashkey, g->hashbd,
  686.        pxx[g->piece], ((g->color == 2) ? "     " : ColorStr[g->color]),
  687.            (is_promoted[g->piece] ? '+' : ' '));
  688.     }
  689.       fclose (fd);
  690. /* Game saved */
  691.       ShowMessage (CP[70]);
  692.     }
  693.   else
  694.     /*ShowMessage ("Could not open file");*/
  695.     ShowMessage (CP[48]);
  696. }
  697.  
  698. void
  699. ListGame (void)
  700. {
  701.   FILE *fd;
  702.   short i, f, t;
  703. #ifndef MSDOS
  704.   time_t when;
  705.   char fname[256], dbuf[256];
  706. #else
  707.   char fname[256];
  708. #endif
  709.  
  710.   if (listfile[0])
  711.     strcpy (fname, listfile);
  712.   else
  713.     {
  714. #ifdef MSDOS
  715.       sprintf (fname, "chess.lst");
  716. #else
  717.       time (&when);
  718.       strncpy (dbuf, ctime (&when), 20);
  719.       dbuf[7] = '\0';
  720.       dbuf[10] = '\0';
  721.       dbuf[13] = '\0';
  722.       dbuf[16] = '\0';
  723.       dbuf[19] = '\0';
  724. /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
  725.    date is Jan 1
  726.    time is 02:03:04
  727.    program played white */
  728.       sprintf (fname, "CLp%s.%s%s-%s%s%s%c", patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14, dbuf + 17, ColorStr[computer][0]);
  729.       /* replace space padding with 0 */
  730.       for (i = 0; fname[i] != '\0'; i++)
  731.     if (fname[i] == ' ')
  732.       fname[i] = '0';
  733. #endif /* MSDOS */
  734.     }
  735.   fd = fopen (fname, "w");
  736.   if (!fd)
  737.     {
  738.       printf (CP[219], fname);
  739.       exit (1);
  740.     }
  741.   /*fprintf (fd, "gnuchess game %d\n", u);*/
  742.   fprintf (fd, CP[161], patchlevel);
  743.   fprintf (fd, CP[10]);
  744.   fprintf (fd, CP[11]);
  745.   for (i = 1; i <= GameCnt; i++)
  746.     {
  747.       f = GameList[i].gmove >> 8;
  748.       t = (GameList[i].gmove & 0xFF);
  749.       algbr (f, t, GameList[i].flags);
  750.       if(GameList[i].flags & book)
  751.           fprintf (fd, "%5s  %5d    Book%7ld %5d", mvstr[0],
  752.            GameList[i].score, 
  753.            GameList[i].nodes, GameList[i].time);
  754.       else
  755.           fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  756.            GameList[i].score, GameList[i].depth,
  757.            GameList[i].nodes, GameList[i].time);
  758.       if ((i % 2) == 0)
  759.     {
  760.       fprintf (fd, "\n");
  761. #ifdef DEBUG40
  762.       if (computer == white)
  763.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  764.              GameList[i].d1,
  765.              GameList[i].d2,
  766.              GameList[i].d3,
  767.              GameList[i].d4,
  768.              GameList[i].d5,
  769.              GameList[i].d6,
  770.              GameList[i].d7);
  771.       else
  772.         fprintf (fd, " %d %d %d %d %d %d %d\n",
  773.              GameList[i - 1].d1,
  774.              GameList[i - 1].d2,
  775.              GameList[i - 1].d3,
  776.              GameList[i - 1].d4,
  777.              GameList[i - 1].d5,
  778.              GameList[i - 1].d6,
  779.              GameList[i - 1].d7);
  780. #endif
  781.     }
  782.       else
  783.     fprintf (fd, "         ");
  784.     }
  785.   fprintf (fd, "\n\n");
  786.   if (GameList[GameCnt].flags & draw)
  787.     {
  788.       fprintf (fd, CP[54], DRAW);
  789.     }
  790.   else if (GameList[GameCnt].score == -(SCORE_LIMIT+999))
  791.     {
  792.       fprintf (fd, "%s\n", ColorStr[player ]);
  793.     }
  794.   else if (GameList[GameCnt].score == (SCORE_LIMIT+998))
  795.     {
  796.       fprintf (fd, "%s\n", ColorStr[player ^ 1]);
  797.     }
  798.   fclose (fd);
  799. }
  800.  
  801. void
  802. Undo (void)
  803.  
  804. /*
  805.  * Undo the most recent half-move.
  806.  */
  807.  
  808. {
  809.   short f, t;
  810.   f = GameList[GameCnt].gmove >> 8;
  811.   t = GameList[GameCnt].gmove & 0x7F;
  812.    if ( f > NO_SQUARES )
  813.      { /* the move was a drop */
  814.         Captured[color[t]][board[t]]++;
  815.     board[t] = no_piece;
  816.     color[t] = neutral;
  817.     Mvboard[t]--;
  818.      }
  819.    else
  820.      {
  821.     if ( GameList[GameCnt].flags & promote )
  822.       board[f] = unpromoted[board[t]];
  823.     else
  824.       board[f] = board[t];
  825.     color[f] = color[t];
  826.     board[t] = GameList[GameCnt].piece;
  827.     color[t] = GameList[GameCnt].color;
  828.     if ( board[t] != no_piece )
  829.       Captured[color[f]][unpromoted[board[t]]]--;
  830.     if (color[t] != neutral)
  831.       Mvboard[t]--;
  832.         Mvboard[f]--;
  833.      }
  834.    InitializeStats ();
  835.  
  836.   if (TCflag && (TCmoves>1))
  837.     ++TimeControl.moves[color[f]];
  838.   hashkey = GameList[GameCnt].hashkey;
  839.   hashbd = GameList[GameCnt].hashbd;
  840.   GameCnt--;
  841.   computer = computer ^ 1;
  842.   opponent = opponent ^ 1;
  843.   flag.mate = false;
  844.   Sdepth = 0;
  845.   player = player ^ 1;
  846.   ShowSidetoMove ();
  847.   UpdateDisplay (0, 0, 1, 0);
  848.   if (flag.regularstart)
  849.   Book = false;
  850. }
  851.  
  852. void
  853.   ShowMoves ()
  854. {
  855. #ifdef NONDSP
  856.   unsigned j;
  857.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  858.     {
  859.       struct leaf *node = &Tree[j];
  860.       algbr (node->f, node->t, node->flags);
  861.       printf ("%s %s %s %s %d 0x%x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node->score,node->flags);
  862.     }
  863. #endif
  864. }
  865.  
  866. void
  867.  TestSpeed (void (*f) (short int side, short int ply), unsigned j)
  868. {
  869. #ifdef test
  870.   unsigned jj;
  871. #endif
  872.   unsigned i;
  873.   long cnt, rate, t1, t2;
  874. #ifdef HASGETTIMEOFDAY
  875. struct timeval tv;
  876. #endif
  877.  
  878. #ifdef HASGETTIMEOFDAY
  879.   gettimeofday(&tv,NULL);
  880.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  881. #else
  882.   t1 = time (0);
  883. #endif
  884.   for (i = 0; i < j; i++)
  885.     {
  886.       f (opponent, 2);
  887. #ifdef test
  888.     for(jj=TrPnt[2];i<TrPnt[3];jj++)if(!pick(jj,TrPnt[3]-1))break;
  889. #endif
  890.     }
  891. #ifdef HASGETTIMEOFDAY
  892.   gettimeofday(&tv,NULL);
  893.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  894. #else
  895.   t2 = time (0);
  896. #endif
  897.   cnt = j * (TrPnt[3] - TrPnt[2]);
  898.   if (t2 - t1)
  899.     et = (t2 - t1);
  900.   else
  901.     et = 1;
  902.   rate = (((et) ? ((cnt*100) / et) : 0));
  903.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  904. #ifdef NONDSP
  905.   printz (CP[91], cnt, rate);
  906. #ifdef DEBUG9
  907.   for (j = TrPnt[2]; j < TrPnt[3]; j++)
  908.     {
  909.       struct leaf *node = &Tree[j];
  910.       algbr (node->f, node->t, node->flags);
  911.       printf ("%s %s %s %s %d %x\n", mvstr[0], mvstr[1], mvstr[2], mvstr[3],node
  912. ->score,node->flags);
  913.     }
  914. #endif
  915. #else
  916.   ShowNodeCnt (cnt);
  917. #endif
  918. }
  919.  
  920. void
  921.  TestPSpeed (short int (*f) (short int side), unsigned j)
  922. {
  923.   short i;
  924.   long cnt, rate, t1, t2;
  925. #ifdef HASGETTIMEOFDAY
  926. struct timeval tv;
  927. #endif
  928.  
  929. #ifdef HASGETTIMEOFDAY
  930.   gettimeofday(&tv,NULL);
  931.   t1 = (tv.tv_sec*100+(tv.tv_usec/10000));
  932. #else
  933.   t1 = time (0);
  934. #endif
  935.   for (i = 0; i < j; i++)
  936.     {
  937.       (void) f (opponent);
  938.     }
  939. #ifdef HASGETTIMEOFDAY
  940.   gettimeofday(&tv,NULL);
  941.   t2 = (tv.tv_sec*100+(tv.tv_usec/10000));
  942. #else
  943.   t2 = time (0);
  944. #endif
  945.   cnt = j;
  946.   if (t2 - t1)
  947.     et = (t2 - t1);
  948.   else
  949.     et = 1;
  950.   rate = (et) ? ((cnt*100) / et) : 0;
  951.   /*printz ("Nodes= %ld Nodes/sec= %ld\n", cnt, rate);*/
  952. #ifdef NONDSP
  953.   printz (CP[91], cnt, rate);
  954. #else
  955.   ShowNodeCnt (cnt);
  956. #endif
  957. }
  958.  
  959.  
  960. void
  961. SetOppTime (char *s)
  962. {
  963.   char *time;
  964.   register tmp = 0;
  965.   int m, t,sec;
  966.   sec = 0;
  967.   time = &s[strlen (CP[197])];
  968.   t = (int)strtol (time, &time, 10);
  969.   if(*time == ':'){time++; sec=(int)strtol(time, &time,10);}
  970.   m = (int)strtol (time, &time, 10);
  971.   if (t) 
  972.     TimeControl.clock[opponent] = t;
  973.   if (m)
  974.     TimeControl.moves[opponent] = m;
  975. #if defined XSHOGI 
  976.   printz (CP[222], m, t);
  977. #endif
  978. }
  979.  
  980.  
  981.  
  982. void
  983. SetMachineTime (char *s)
  984. {
  985.   char *time;
  986.   long tmp = 0;
  987.   int m, t,sec;
  988.   sec = 0;
  989.   time = &s[strlen (CP[197])];
  990.   t = (int)strtol (time, &time, 10);
  991.   if(*time == ':'){time++; sec=(int)strtol(time, &time,10);}
  992.   m = (int)strtol (time, &time, 10);
  993.   if (t)
  994.     TimeControl.clock[computer] = t;
  995.   if (m)
  996.     TimeControl.moves[computer] = m;
  997. #if defined XSHOGI 
  998.   printz (CP[222], m, t);
  999. #endif
  1000. }
  1001.  
  1002.  
  1003. #ifdef DEBUG_EVAL
  1004. short debug_eval = false;
  1005. FILE  *debug_eval_file = NULL;
  1006. #endif
  1007.  
  1008.  
  1009. void
  1010. InputCommand (void)
  1011.  
  1012. /*
  1013.  * Process the users command. If easy mode is OFF (the computer is thinking
  1014.  * on opponents time) and the program is out of book, then make the 'hint'
  1015.  * move on the board and call SelectMove() to find a response. The user
  1016.  * terminates the search by entering ^C (quit siqnal) before entering a
  1017.  * command. If the opponent does not make the hint move, then set Sdepth to
  1018.  * zero.
  1019.  */
  1020.  
  1021. {
  1022.   int eof = 0;
  1023.   short have_shown_prompt = false;
  1024.   short ok;
  1025.   unsigned short mv;
  1026.   char s[80], sx[80];
  1027.  
  1028.   ok = flag.quit = false;
  1029.   player = opponent;
  1030. #if ttblsz
  1031.   if(TTadd > ttblsize)ZeroTTable();
  1032. #endif
  1033.   if (hint > 0 && !flag.easy && !flag.force )
  1034.       {
  1035.     ft = time0;
  1036.     fflush (stdout);
  1037.     algbr ((short) hint >> 8, (short) hint & 0xff, false);
  1038.     strcpy (s, mvstr[0]);
  1039. #ifdef DEBUG12
  1040. #include "debug12.h"
  1041. #endif
  1042. #ifdef DEBUG40
  1043. whichway = 0;
  1044. #endif
  1045.     if (flag.post) GiveHint ();
  1046.     if (VerifyMove (s, 1, &mv))
  1047.       {
  1048.         Sdepth = 0;
  1049. #ifdef QUIETBACKGROUND
  1050. #ifdef NONDSP
  1051.         PromptForMove ();
  1052. #else
  1053.         ShowSidetoMove ();
  1054.         ShowPrompt ();
  1055. #endif
  1056.         have_shown_prompt = true;
  1057. #endif /* QUIETBACKGROUND */
  1058.         SelectMove (computer, 2);
  1059.         VerifyMove (s, 2, &mv);
  1060.         Sdepth = 0;
  1061.       }
  1062. #ifdef DEBUG40
  1063.     else whichway = 1;
  1064. #endif
  1065.     time0 = ft;
  1066.       }
  1067.   while (!(ok || flag.quit))
  1068.     {
  1069.       player = opponent;
  1070. #ifdef QUIETBACKGROUND
  1071.       if (!have_shown_prompt)
  1072.     {
  1073. #endif /* QUIETBACKGROUND */
  1074. #ifdef NONDSP
  1075.       PromptForMove ();
  1076. #else
  1077.       ShowSidetoMove ();
  1078.       ShowPrompt ();
  1079. #endif
  1080. #ifdef QUIETBACKGROUND
  1081.     }
  1082.       have_shown_prompt = false;
  1083. #endif /* QUIETBACKGROUND */
  1084. #ifdef NONDSP
  1085.       s[0] = sx[0] = '\0';
  1086.       while (!sx[0])
  1087.     (void) gets (sx);
  1088. #else
  1089.       fflush (stdout);
  1090. #if defined MSDOS || defined THINK_C
  1091.       s[0] = '\0';
  1092.       eof = ( gets (sx) == NULL );
  1093. #else
  1094.       eof = ( getstr (sx) == ERR );
  1095. #endif
  1096. #endif
  1097.       sscanf (sx, "%s", s);
  1098.       if (eof)
  1099.     ExitChess ();
  1100.       if (s[0] == '\0')
  1101.     continue;
  1102.       if (strcmp (s, CP[131]) == 0)    /*bd*/
  1103.     {
  1104. #if defined XSHOGI
  1105.       xshogi = 0;
  1106. #endif
  1107.       ClrScreen ();
  1108.       UpdateDisplay (0, 0, 1, 0);
  1109. #if defined XSHOGI
  1110.       xshogi = 1;
  1111. #endif
  1112.     }
  1113.       else if (strcmp (s, CP[129]) == 0) /* noop */ ;    /*alg*/
  1114.       else if ((strcmp (s, CP[180]) == 0) || (strcmp (s, CP[216]) == 0))    /* quit exit*/
  1115.     flag.quit = true;
  1116.       else if (strcmp (s, CP[178]) == 0)    /*post*/
  1117.     {
  1118.       flag.post = !flag.post;
  1119.     }
  1120.       else if ((strcmp (s, CP[191]) == 0) || (strcmp (s, CP[154]) == 0))    /*set edit*/
  1121.     EditBoard ();
  1122. #ifdef NONDSP
  1123.       else if (strcmp (s, CP[190]) == 0)    /*setup*/
  1124.     SetupBoard ();
  1125. #endif
  1126.       else if (strcmp (s, CP[156]) == 0)    /*first*/
  1127.     {
  1128.       ok = true;
  1129.     }
  1130.       else if (strcmp (s, CP[162]) == 0)    /*go*/
  1131.     {
  1132.       ok = true;
  1133.       flag.force = false;
  1134.       if (computer == black)
  1135.         {
  1136.           computer = white;
  1137.           opponent = black;
  1138.         }
  1139.       else
  1140.         {
  1141.           computer = black;
  1142.           opponent = white;
  1143.         }
  1144.     }
  1145.       else if (strcmp (s, CP[166]) == 0)    /*help*/
  1146.     help ();
  1147.       else if (strcmp (s, CP[221]) == 0)    /*material*/
  1148.     flag.material = !flag.material;
  1149.       else if (strcmp (s, CP[157]) == 0)    /*force*/
  1150.     {flag.force = !flag.force; flag.bothsides = false;}
  1151.       else if (strcmp (s, CP[134]) == 0)    /*book*/
  1152.     Book = Book ? 0 : BOOKFAIL;
  1153.       else if (strcmp (s, CP[172]) == 0)    /*new*/
  1154.     {
  1155.       NewGame ();
  1156.       UpdateDisplay (0, 0, 1, 0);
  1157.     }
  1158.       else if (strcmp (s, CP[171]) == 0)    /*list*/
  1159.     ListGame ();
  1160.       else if (strcmp (s, CP[169]) == 0 || strcmp (s, CP[217]) == 0)    /*level clock*/
  1161.     SelectLevel (sx);
  1162.       else if (strcmp (s, CP[165]) == 0)    /*hash*/
  1163.     flag.hash = !flag.hash;
  1164.       else if (strcmp (s, CP[227]) == 0)    /*gamein*/
  1165.     flag.gamein = !flag.gamein;
  1166.       else if (strcmp (s, CP[226]) == 0)    /*beep*/
  1167.     flag.beep = !flag.beep;
  1168.       else if (strcmp (s, CP[197]) == 0)    /*time*/
  1169.     { SetMachineTime (sx); }
  1170.       else if (strcmp (s, CP[228]) == 0)    /*time*/
  1171.     { SetOppTime (sx); }
  1172.       else if (strcmp (s, CP[33]) == 0)    /*Awindow*/
  1173.     ChangeAlphaWindow ();
  1174.       else if (strcmp (s, CP[39]) == 0)    /*Bwindow*/
  1175.     ChangeBetaWindow ();
  1176.       else if (strcmp (s, CP[183]) == 0)    /*rcptr*/
  1177.     flag.rcptr = !flag.rcptr;
  1178.       else if (strcmp (s, CP[168]) == 0)    /*hint*/
  1179.     GiveHint ();
  1180.       else if (strcmp (s, CP[135]) == 0)    /*both*/
  1181.     {
  1182.       flag.bothsides = !flag.bothsides;
  1183.           flag.force = false;
  1184.       Sdepth = 0;
  1185.       ElapsedTime (1);
  1186.       SelectMove (opponent, 1);
  1187.       ok = true;
  1188.     }
  1189.       else if (strcmp (s, CP[185]) == 0)    /*reverse*/
  1190.     {
  1191.       flag.reverse = !flag.reverse;
  1192.       ClrScreen ();
  1193.       UpdateDisplay (0, 0, 1, 0);
  1194.     }
  1195.       else if (strcmp (s, CP[195]) == 0)    /*switch*/
  1196.     {
  1197.       computer = computer ^ 1;
  1198.       opponent = opponent ^ 1;
  1199.       xwndw = (computer == black) ? WXWNDW : BXWNDW;
  1200.       flag.force = false;
  1201.       Sdepth = 0;
  1202.       ok = true;
  1203.     }
  1204.       else if (strcmp (s, CP[203]) == 0)    /*black*/
  1205.     {
  1206.       computer = white;
  1207.       opponent = black;
  1208.       xwndw = WXWNDW;
  1209.       flag.force = false;
  1210.       Sdepth = 0;
  1211.  
  1212.       /*
  1213.            * ok = true; don't automatically start with black command
  1214.            */
  1215.     }
  1216.       else if (strcmp (s, CP[133]) == 0)    /*white*/
  1217.     {
  1218.       computer = black;
  1219.       opponent = white;
  1220.       xwndw = BXWNDW;
  1221.       flag.force = false;
  1222.       Sdepth = 0;
  1223.  
  1224.       /*
  1225.            * ok = true; don't automatically start with white command
  1226.            */
  1227.     }
  1228.       else if (strcmp (s, CP[201]) == 0 && GameCnt > 0)    /*undo*/
  1229.     {
  1230.       Undo ();
  1231.     }
  1232.       else if (strcmp (s, CP[184]) == 0 && GameCnt > 1)    /*remove*/
  1233.     {
  1234.       Undo ();
  1235.       Undo ();
  1236.     }
  1237.       else if (strcmp (s, CP[160]) == 0)    /*get*/
  1238.     GetGame ();
  1239.       else if (strcmp (s, CP[207]) == 0)    /*xget*/
  1240.     GetXGame ();
  1241.       else if (strcmp (s, CP[189]) == 0)    /*save*/
  1242.     SaveGame ();
  1243.       else if (strcmp (s, CP[151]) == 0)    /*depth*/
  1244.     ChangeSearchDepth ();
  1245. #ifdef DEBUG
  1246.       else if (strcmp (s, CP[147]) == 0)    /*debuglevel*/
  1247.     ChangeDbLev ();
  1248. #endif /* DEBUG */
  1249.       else if (strcmp (s, CP[164]) == 0)    /*hashdepth*/
  1250.     ChangeHashDepth ();
  1251.       else if (strcmp (s, CP[182]) == 0)    /*random*/
  1252.     dither = DITHER;
  1253.       else if (strcmp (s, CP[229]) == 0)    /*hard*/
  1254.     flag.easy = false;
  1255.       else if (strcmp (s, CP[152]) == 0)    /*easy*/
  1256.     flag.easy = !flag.easy;
  1257.       else if (strcmp (s, CP[230]) == 0)    /*tsume*/
  1258.     flag.tsume = !flag.tsume;
  1259.       else if (strcmp (s, CP[143]) == 0)    /*contempt*/
  1260.     SetContempt ();
  1261.       else if (strcmp (s, CP[209]) == 0)    /*xwndw*/
  1262.     ChangeXwindow ();
  1263.       else if (strcmp (s, CP[186]) == 0)    /*rv*/
  1264.     {
  1265.       flag.rv = !flag.rv;
  1266.       UpdateDisplay (0, 0, 1, 0);
  1267.     }
  1268.       else if (strcmp (s, CP[145]) == 0)    /*coords*/
  1269.     {
  1270.       flag.coords = !flag.coords;
  1271.       UpdateDisplay (0, 0, 1, 0);
  1272.     }
  1273.       else if (strcmp (s, CP[193]) == 0)    /*stars*/
  1274.     {
  1275.       flag.stars = !flag.stars;
  1276.       UpdateDisplay (0, 0, 1, 0);
  1277.     }
  1278.       else if (strcmp (s, CP[5]) == 0)            /*moves*/
  1279.     {
  1280.       ShowMessage (CP[108]);/*test movelist*/
  1281.       TestSpeed (MoveList, 1);
  1282.       ShowMessage (CP[107]);/*test capturelist*/
  1283.       TestSpeed (CaptureList, 1);
  1284.       ShowMessage (CP[85]);/*test score position*/
  1285.           ExaminePosition();
  1286.       TestPSpeed (ScorePosition, 1);
  1287.     }
  1288.       else if (strcmp (s, CP[196]) == 0)    /*test*/
  1289.     {
  1290.       ShowMessage (CP[108]);/*test movelist*/
  1291.       TestSpeed (MoveList, 20000);
  1292.       ShowMessage (CP[107]);/*test capturelist*/
  1293.       TestSpeed (CaptureList, 30000);
  1294.       ShowMessage (CP[85]);/*test score position*/
  1295.           ExaminePosition();
  1296.       TestPSpeed (ScorePosition, 15000);
  1297.     }                                             
  1298. #ifdef DEBUG_EVAL
  1299.       else if (strcmp (s, "eval") == 0)        /*eval*/
  1300.     {             
  1301.       debug_eval = true;
  1302.       if ( debug_eval_file = fopen(EVALFILE,"w") ) {
  1303.         ExaminePosition();
  1304.         fprintf (debug_eval_file, "\nscoring for %s to move...\n\n",
  1305.         ColorStr[player]);
  1306.         ScorePosition (player);
  1307.         fclose (debug_eval_file);
  1308.       }
  1309.       debug_eval = false;
  1310.     }        
  1311.       else if (strcmp (s, "pattern") == 0)        /*pattern*/
  1312.     {             
  1313.       debug_eval = true;
  1314.       if ( debug_eval_file = fopen(EVALFILE,"w") ) {
  1315.         short side;
  1316.         for (side=black; side<=white; side++) {
  1317.           extern short ScorePatternDistance();
  1318.           short s = ScorePatternDistance (side); 
  1319.           fprintf (debug_eval_file, "\npattern distance score for %s is %d\n\n",
  1320.          ColorStr[side], s);
  1321.         }
  1322.         fclose (debug_eval_file);
  1323.       }
  1324.       debug_eval = false;
  1325.     }        
  1326. #endif
  1327.       else
  1328.       if (strcmp (s, CP[179]) == 0)    /*p*/
  1329.     ShowPostnValues ();
  1330.       else if (strcmp (s, CP[148]) == 0)    /*debug*/
  1331.     DoDebug ();
  1332.       else
  1333.     {
  1334.       if ( flag.mate )
  1335.         ok = true;
  1336.       else
  1337.         ok = VerifyMove (s, 0, &mv);
  1338.       Sdepth = 0;
  1339.     }
  1340.     }
  1341.  
  1342.   ElapsedTime (1);
  1343.   if (flag.force)
  1344.     {
  1345.       computer = opponent;
  1346.       opponent = computer ^ 1;
  1347.     }
  1348. #if defined XSHOGI 
  1349.   printz ("%d. %s\n", ++mycnt2, s);
  1350. #ifdef notdef /* optional pass best line to frontend with move */
  1351.   if (flag.post)
  1352.     {
  1353.       register int i;
  1354.  
  1355.       printz (" %6d ", MSCORE);
  1356.       for (i = 1; MV[i] > 0; i++)
  1357.     {
  1358.       algbr ((short) (MV[i] >> 8), (short) (MV[i] & 0xFF), false);
  1359.       printz ("%5s ", mvstr[0]);
  1360.     }
  1361.     }
  1362.   printz ("\n");
  1363. #endif
  1364. #endif /* XSHOGI */
  1365.   signal (SIGINT, TerminateSearch);
  1366. #if !defined MSDOS && !defined THINK_C
  1367.   signal (SIGQUIT, TerminateSearch);
  1368. #endif /* MSDOS */
  1369. }
  1370.  
  1371.  
  1372. #ifdef NOFIONREAD
  1373. #ifdef FIONREAD
  1374. #undef FIONREAD
  1375. #endif
  1376. #endif
  1377.  
  1378.  
  1379. #ifdef HASGETTIMEOFDAY
  1380. void
  1381. ElapsedTime (short int iop)
  1382.  
  1383.  
  1384. /*
  1385.  * Determine the time that has passed since the search was started. If the
  1386.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1387.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1388.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1389.  */
  1390.  
  1391. {
  1392. struct timeval tv;
  1393. #ifndef MSDOS
  1394.   int nchar;
  1395.   extern int errno;
  1396.   int i;
  1397. #ifdef FIONREAD
  1398.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1399.     {
  1400.       perror ("FIONREAD");
  1401.       fprintf (stderr,
  1402.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1403.     i, errno, FIONREAD);
  1404.       exit (1);
  1405.     }
  1406.  
  1407.   if (nchar)
  1408.     {
  1409.       if (!flag.timeout)
  1410.     flag.back = true;
  1411.       flag.bothsides = false;
  1412.     }
  1413. #endif /*FIONREAD*/
  1414. #else
  1415.   if (kbhit ())
  1416.     {
  1417.       if (!flag.timeout)
  1418.     flag.back = true;
  1419.       flag.bothsides = false;
  1420.     }
  1421. #endif /* MSDOS */
  1422.   gettimeofday(&tv,NULL);
  1423.   et = (tv.tv_sec*100+(tv.tv_usec/10000)) - time0;
  1424.   ETnodes = NodeCnt + ZNODES;
  1425.   if (et < 0)
  1426.     et = 0;
  1427.   if (iop == 1)
  1428.     {
  1429.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1430.     flag.timeout = true;
  1431.       ETnodes = NodeCnt + ZNODES;
  1432.       gettimeofday(&tv,NULL);
  1433.       time0 = tv.tv_sec*100+tv.tv_usec/10000;
  1434.     }
  1435. #if !defined NONDSP
  1436. #ifdef QUIETBACKGROUND
  1437.   if (!background)
  1438. #endif /* QUIETBACKGROUND */
  1439.     UpdateClocks ();
  1440. #endif
  1441. }
  1442. #else
  1443. void
  1444. ElapsedTime (short int iop)
  1445.  
  1446.  
  1447. /*
  1448.  * Determine the time that has passed since the search was started. If the
  1449.  * elapsed time exceeds the target (ResponseTime+ExtraTime) then set timeout
  1450.  * to true which will terminate the search. iop = 0 calculate et bump ETnodes
  1451.  * iop = 1 calculate et set timeout if time exceeded, calculate et
  1452.  */
  1453.  
  1454. {
  1455. #ifndef MSDOS
  1456.   int nchar;
  1457.   extern int errno;
  1458.   int i;
  1459. #ifdef FIONREAD
  1460.   if (i = ioctl ((int) 0, FIONREAD, &nchar))
  1461.     {
  1462.       perror ("FIONREAD");
  1463.       fprintf (stderr,
  1464.         "You probably have a non-ANSI <ioctl.h>; see README. %d %d %x\n",
  1465.     i, errno, FIONREAD);
  1466.       exit (1);
  1467.     }
  1468.  
  1469.   if (nchar)
  1470.     {
  1471.       if (!flag.timeout)
  1472.     flag.back = true;
  1473.       flag.bothsides = false;
  1474.     }
  1475. #endif /*FIONREAD*/
  1476. #else
  1477.   if (kbhit ())
  1478.     {
  1479.       if (!flag.timeout)
  1480.     flag.back = true;
  1481.       flag.bothsides = false;
  1482.     }
  1483. #endif /* MSDOS */
  1484. #if defined THINK_C
  1485.   et = (time ((time_t *) 0) - time0) * 100;
  1486. #else
  1487.   et = (time ((long *) 0) - time0) * 100;
  1488. #endif
  1489.   ETnodes = NodeCnt + ZNODES;
  1490.   if (et < 0)
  1491.     et = 0;
  1492.   if (iop == 1)
  1493.     {
  1494.       if (et > ResponseTime + ExtraTime && Sdepth > MINDEPTH)
  1495.     flag.timeout = true;
  1496.       ETnodes = NodeCnt + ZNODES;
  1497.       time0 = time ((time_t *) 0);
  1498.     }
  1499. #if !defined NONDSP
  1500. #ifdef QUIETBACKGROUND
  1501.   if (!background)
  1502. #endif /* QUIETBACKGROUND */
  1503.     UpdateClocks ();
  1504. #endif
  1505. }
  1506. #endif
  1507. void
  1508. SetTimeControl (void)
  1509. {
  1510.   if (TCflag)
  1511.     {
  1512.       TimeControl.moves[black] = TimeControl.moves[white] = TCmoves;
  1513.       TimeControl.clock[black] += 6000L * TCminutes + TCseconds * 100;
  1514.       TimeControl.clock[white] += 6000L * TCminutes + TCseconds * 100;
  1515.     }
  1516.   else
  1517.     {
  1518.       TimeControl.moves[black] = TimeControl.moves[white] = 0;
  1519.       TimeControl.clock[black] = TimeControl.clock[white] = 0;
  1520.     }
  1521.   flag.onemove = (TCmoves == 1);
  1522.   et = 0;
  1523.   ElapsedTime (1);
  1524. }
  1525.